#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGridLayout>
class PicturePuzzleItem;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void init();
    void disPlay();
    bool isSucceed();

    void paintEvent(QPaintEvent *event) override;  // 核心绘制函数
    void resizeEvent(QResizeEvent *event) override; // 处理窗口缩放

    void setBlockSize(int x, int y);

protected:
    void mousePressEvent(QMouseEvent *event) override;  // 新增点击事件处理

private:
    int findItemIndexByPos(int gridX, int gridY) const;  // 根据逻辑位置找索引
    bool isAdjacentToBlank(int index) const;             // 邻接判断
    void swapWithBlank(int index);                      // 交换位置

    Ui::MainWindow *ui;
    QList<PicturePuzzleItem*> m_items;
    int m_blockSizeX = 100;                      // 拼图块基准尺寸
    int m_blockSizeY = 100;
    QGridLayout *m_gridLayout;
    PicturePuzzleItem* m_blackItem;
};

#endif // MAINWINDOW_H

